package com.xxh.cloud.provider.service.impl;



import com.xxh.cloud.common.util.DateUtil;
import com.xxh.cloud.common.util.FileUtil;
import com.xxh.cloud.common.vo.R;
import com.xxh.cloud.provider.entry.OssLog;
import com.xxh.cloud.provider.mapper.OssLogMapper;
import com.xxh.cloud.provider.service.inft.OssLogService;
import com.xxh.cloud.provider.until.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Service
public class OssLogServiceImpl implements OssLogService {
    @Autowired
    private OssLogMapper mapper;

    @Override
    public R uploadImg(MultipartFile file) throws IOException {
        //1.校验
        if(!file.isEmpty()){
            //2.获取上传的文件名
            String fn= FileUtil.rename(file.getOriginalFilename());
            //3.获取上传的文件内容
            byte[] data=file.getBytes();
            //4.生成url的有效期
            Date date= DateUtil.getTime(1);
            //4.将资源上传给OSS
            String url= AliOssUtil.upload(data,fn,date);
            //5.验证上传是否成功
            if(StringUtils.hasLength(url)){
                //6.上传成功，记录操作日志
                OssLog log=new OssLog(AliOssUtil.BUCKET,fn,url,1,new Date(),date);
                //mapper.save(log);
                //返回
                return R.ok(log);
            }
        }
        return R.fail();
    }

    @Override
    public R all() {
        return R.ok();
    }
}
